//author:jxx
//此处是对表单的方法，组件，权限操作按钮等进行任意扩展(方法扩展可参照SellOrder.js)
//import { ThirdPartyDraggable } from "@fullcalendar/interaction";
import modelFooter from "./user/Small_user_contact.vue";
import store from "@/store/index";
let extension = {
  components: {//动态扩充组件或组件路径
    //表单header、content、footer对应位置扩充的组件
    gridHeader: '',//{ template: "<div>扩展组xx件</div>" },
    gridBody: () => import("./user/addSite2.vue"),
    gridFooter: '',
    //弹出框(修改、编辑、查看)header、content、footer对应位置扩充的组件
    modelHeader: '',
    modelBody: '',
    modelFooter: modelFooter
  },
  buttons: { view: [], box: [], detail: [] },//扩展的按钮
  data () {
    return {
      cityAreas: null,
    }
  },

  methods: {
    getFormOption (field) {
      let option;
      this.editFormOptions.forEach(x => {
        x.forEach(item => {
          if (item.field == field) {
            option = item;
          }
        })
      })
      return option;
    },
    getSearchFormOptions (field) {
      let option;
      this.searchFormOptions.forEach(x => {
        x.forEach(item => {
          if (item.field == field) {
            option = item;
          }
        })
      })
      return option;
    },
    onInit () {
      this.$store.getters.data().viewPageTest = this;

      this.boxOptions.height = 800;
      this.boxOptions.width = 1000;
      var selectOption = this.getFormOption('membertype');
      var id_card = this.getFormOption('id_card');
      var memberlevel = this.getFormOption('memberlevel');
      var certificate_type = this.getFormOption('certificate_type');
      let address = this.getFormOption('address');
      let isCabinetKeeper = this.getFormOption("isCabinetKeeper");
      let isDataKeeper = this.getFormOption("isDataKeeper");
      let _userInfo = store.getters.getUserInfo();
      if (_userInfo.roleID == 1) {
        isCabinetKeeper.hidden = false;
        isDataKeeper.hidden = false;
        // merchantCodeoption.hidden=false;
      } else {
        isCabinetKeeper.hidden = true;
        isDataKeeper.hidden = true;
        //merchantCodeoption.hidden=true;
      }
      // 从表添加按钮
      this.columns.push({
        title: "操作",
        width: 165,
        render: (h, { row, column, index }) => {
          return h(
            "a",
            {
              props: {}, style: {},
              on: {
                click: () => {
                  // console.log("查看生成场地", row.mall_site);

                  this.$refs.gridBody.open(row)
                }
              }
            },
            "会员充值"
          )

        }
      })
      // 会员类型
      selectOption.onChange = (val, item) => {
        let parmas = {
          order: "desc",
          page: 1,
          rows: 100,
          sort: "membertypeid",
          value: val,
          wheres: "[]",
        }
        memberlevel.data = []
        this.http.post('/api/MebType/getDetailPage', parmas).then(res => {
          if (res.rows != null && res.rows.length != 0) {
            res.rows.forEach(i => {
              let obj = {
                key: i.levelid.toString(),
                value: i.level
              }
              memberlevel.data.push(obj)
            })
          }
        })
      }
      let searchLX = this.getSearchFormOptions('membertype')
      let searchDJ = this.getSearchFormOptions('memberlevel')
      // 查询会员类型
      searchLX.onChange = (val, item) => {
        let parmas = {
          order: "desc",
          page: 1,
          rows: 100,
          sort: "membertypeid",
          value: val,
          wheres: "[]",
        }
        searchDJ.data = []
        this.http.post('/api/MebType/getDetailPage', parmas).then(res => {
          if (res.rows != null && res.rows.length != 0) {
            res.rows.forEach(i => {
              let obj = {
                key: i.levelid.toString(),
                value: i.level
              }
              searchDJ.data.push(obj)
            })
          }
        })
      }

      this.http.post('/api/Dictionary/GetVueDictionary', ['areas']).then(res => {
        this.cityAreas = res[0].data
        address.data = res[0].data
        // 证件类型
        certificate_type.onChange = value => {
          if (value == 1) {
            // 证件号
            id_card.onBlur = val => {
              if (val.length != 18) {
                this.$message.error('身份证输入错误！')
              } else {
                // 获取城市
                this.cityAreas.forEach(i => {
                  if (val.slice(0, 6) == i.key) {
                    this.editFormFields.provinCity = i.key
                  }
                })
                //获取性别
                if (parseInt(val.substr(16, 1)) % 2 == 1) {
                  //男
                  this.editFormFields.sex = '1'
                } else {
                  //女
                  this.editFormFields.sex = '2'
                }
                // 321281192601061015  13545625874
                //获取出生日期
                this.editFormFields.birthday = val.substring(6, 10) + "-" + val.substring(10, 12) + "-" + val.substring(12, 14);
                // 年龄
                var strBirthday = val.substr(6, 4) + "/" + val.substr(10, 2) + "/" + val.substr(12, 2);
                var birthDate = new Date(strBirthday);
                var nowDateTime = new Date();
                var age = nowDateTime.getFullYear() - birthDate.getFullYear();
                //再考虑月、天的因素;.getMonth()获取的是从0开始的，这里进行比较，不需要加1
                if (nowDateTime.getMonth() < birthDate.getMonth() || (nowDateTime.getMonth() == birthDate.getMonth() && nowDateTime.getDate() < birthDate.getDate())) {
                  age--;
                }
                this.editFormFields.age = age
                this.editFormFields.countries = 'CHN'
              }
            }
          } else {
            id_card.onBlur = val => { }
          }
        }
      })
    },
    searchBefore (e) {
      // 是否禁用
      let v = JSON.parse((JSON.stringify(e.wheres)))
      for (let i of v) {
        if (i.name == 'disabled_status') {
          i.value = i.value == 0 ? 0 : 1
        }
      }
      e.wheres = [
        ...v
      ]
      return true;
    },
    searchAfter (e) {
      // 会员等级
      let parmas = {
        order: "desc",
        page: 1,
        rows: 100,
        sort: "membertypeid",
        wheres: "[]",
      }
      this.http.post('/api/MebLevel/GetPageData', parmas).then(res => {
        res.rows.forEach(i => {
          e.forEach(j => {
            if (i.levelid == j.memberlevel) {
              j.memberlevelname = i.level
            }
          })
        })
      })
      // 省 市 区
      e.forEach(i => {
        let reg = /.+?(省|市|自治区|自治州|县|区)/g
        if (i.id_card) {
          let code = i.id_card.slice(0, 6)
          let name = ''
          this.cityAreas.forEach(j => {
            if (code == j.key) {
              name = j.value
              i.countriesName = name.match(reg)[0]
              i.provinceName = name.match(reg)[1]
              i.cityName = name.match(reg)[2]
            }
          })
        }
      })
      return true;
    },
    rowClick ({ row, column, event }) { //查询界面table点击行选中当前行
      this.$refs.table.$refs.table.toggleRowSelection(row);
      return true;
    },
    async modelOpenAfter (row) {
      this.$refs.modelFooter.tabView = '1'
      if (this.$refs.modelFooter) {
        this.$refs.modelFooter.member = row
        if (this.currentAction == 'Add') {
          this.editFormFields.disabled_status = 0
          if (this.$refs.modelFooter.$refs.tableList) {
            this.$refs.modelFooter.$refs.tableList.reset();
          }
          // 会员卡
          if (this.$refs.modelFooter.$refs.tableList2) {
            this.$refs.modelFooter.$refs.tableList2.reset();
          }
          // 券
          if (this.$refs.modelFooter.$refs.tableList3) {
            this.$refs.modelFooter.$refs.tableList3.reset();
          }
          // 消费记录
          if (this.$refs.modelFooter.$refs.tableList4) {
            this.$refs.modelFooter.$refs.tableList4.reset();
          }
          // 储值账户
          if (this.$refs.modelFooter.$refs.tableList5) {
            this.$refs.modelFooter.$refs.tableList5.reset();
          }
          // 积分账户
          if (this.$refs.modelFooter.$refs.tableList6) {
            this.$refs.modelFooter.$refs.tableList6.reset();
          }
          // 车辆信息
          if (this.$refs.modelFooter.$refs.tableList7) {
            this.$refs.modelFooter.$refs.tableList7.reset();
          }
        } else {
          if (row.certificate_type == "1") {
            if (row.id_card.length == 18) {
              // 获取城市
              this.cityAreas.forEach(i => {
                if (row.id_card.slice(0, 6) == i.key) {
                  this.editFormFields.provinCity = i.key
                }
              })
              //获取性别
              if (parseInt(row.id_card.substr(16, 1)) % 2 == 1) {
                //男
                this.editFormFields.member_sex = '1'
              } else {
                //女
                this.editFormFields.member_sex = '2'
              }
              //获取出生日期
              this.editFormFields.birthday = row.id_card.substring(6, 10) + "-" + row.id_card.substring(10, 12) + "-" + row.id_card.substring(12, 14);
              // 年龄
              var strBirthday = row.id_card.substr(6, 4) + "/" + row.id_card.substr(10, 2) + "/" + row.id_card.substr(12, 2);
              var birthDate = new Date(strBirthday);
              var nowDateTime = new Date();
              var age = nowDateTime.getFullYear() - birthDate.getFullYear();
              //再考虑月、天的因素;.getMonth()获取的是从0开始的，这里进行比较，不需要加1
              if (nowDateTime.getMonth() < birthDate.getMonth() || (nowDateTime.getMonth() == birthDate.getMonth() && nowDateTime.getDate() < birthDate.getDate())) {
                age--;
              }
              this.editFormFields.member_age = age
              this.editFormFields.countries = 'CHN'
            }
          }
          if (row.prerogative_begintime || row.prerogative_endtime) {
            this.editFormFields.prerogative_begintime = [row.prerogative_begintime, row.prerogative_endtime]
          }
          this.cityAreas.forEach(i => {
            let w = row.province + row.city + row.area
            if (w == i.key) {
              this.editFormFields.provinCity = i.key
            }
          })

          var memberlevel = this.getFormOption('memberlevel');
          let parmas = {
            order: "desc",
            page: 1,
            rows: 100,
            sort: "membertypeid",
            value: row.membertype,
            wheres: "[]",
          }
          memberlevel.data = []
          this.http.post('/api/MebType/getDetailPage', parmas).then(res => {
            if (res.rows != null && res.rows.length != 0) {
              res.rows.forEach(i => {
                let obj = {
                  key: i.levelid.toString(),
                  value: i.level
                }
                memberlevel.data.push(obj)
              })
            }
          })
          this.$refs.modelFooter.tabView = await '2'
          this.$refs.modelFooter.tabView = await '3'
          this.$refs.modelFooter.tabView = await '4'
          this.$refs.modelFooter.tabView = await '5'
          this.$refs.modelFooter.tabView = await '6'
          this.$refs.modelFooter.tabView = await '7'
          this.$refs.modelFooter.tabView = await '1'
          this.$refs.modelFooter.getTable()
          this.$refs.modelFooter.$refs.tableList2.load();
          this.$refs.modelFooter.$refs.tableList3.load();
          this.$refs.modelFooter.$refs.tableList4.load();
          this.$refs.modelFooter.$refs.tableList5.load();
          this.$refs.modelFooter.$refs.tableList6.load();
          this.$refs.modelFooter.$refs.tableList7.load();
        }
      }
    },
    addBefore (e) {
      this.setParams(e)
      return true
    },
    updateBefore (e) {
      this.setParams(e)
      e.detailData = null
      return true
    },
    setParams (e) {
      let v = JSON.parse(JSON.stringify(e.mainData))
      if (e.mainData.prerogative_begintime) {
        v.prerogative_begintime = e.mainData.prerogative_begintime.split(',')[0]
        v.prerogative_endtime = e.mainData.prerogative_begintime.split(',')[1]
      }
      if (e.mainData.membertype == undefined || e.mainData.memberlevel == undefined) {
        v.membertype = ''
        v.memberlevel = ''
      }
      if (e.mainData.provinCity == undefined) {
        v.province = ''
        v.city = ''
        v.area = ''
      } else {
        v.province = e.mainData.provinCity.slice(0, 2)
        v.city = e.mainData.provinCity.slice(2, 4)
        v.area = e.mainData.provinCity.slice(4, 6)
      }
      e.mainData = {
        ...v
      }
      e.detailData = []
    }
  }
};
export default extension;